package com.tlc.servlet;
import com.tlc.bean.Root;
import com.tlc.dao.RootDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends BossServlet {
    private RootDao rootDao = new RootDao();

    /**
     * 用户登录页
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收请求
        String username = request.getParameter("username");
        String psw = request.getParameter("password");
        int password = Integer.parseInt(psw);
        //获取选中密码的状态值
        String repu = request.getParameter("repu");

        //将用户名密码封装到Root对象中
        Root root = new Root(username, password);
        //获取用户输入的验证码
        String verCode = request.getParameter("verCode");
        //创建会话对象
        HttpSession session = request.getSession();
        //获取验证码的状态值
        String code = (String) session.getAttribute("verCode");

        //校验验证码
        if (code.equalsIgnoreCase(verCode)) {
            //调用Dao层
            Root rt = rootDao.loginDao(username, password);
            //给出结果响应
            if (rt != null) {
                //设置Cookie对象
                Cookie cookie1 = new Cookie("username", username);
                Cookie cookie2 = new Cookie("password", psw);
                //设置cookie的使用范围
                cookie1.setPath("/BMS/login.jsp");
                cookie2.setPath("/BMS/login.jsp");

                //根据是否选中记住密码判断
                if ("on".equals(repu)) {
                    //设置cookie的保存时间
                    cookie1.setMaxAge(60 * 60 * 24 * 7);
                    cookie2.setMaxAge(60 * 60 * 24 * 7);
                }else {
                    //如果没选中设置cookie的保存时间为0
                    cookie1.setMaxAge(0);
                    cookie2.setMaxAge(0);
                }

                //发送cookie
                response.addCookie(cookie1);
                response.addCookie(cookie2);

                //将Root对象保存到session域对象中
                session.setAttribute("root", root);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            } else {
                //设置提示信息
                request.setAttribute("error", "用户名或密码输入有误！");
                //请求跳转
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("error", "验证码输入有误！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }

    protected void quitInterface(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //销毁session对象
        request.getSession().invalidate();
        //同时跳转到登录界面
        response.sendRedirect("login.jsp");
    }
}
